<template>
	<view class="page page-load flex-column-center">
		<!-- 倒计时 -->
		<!-- <view class="time" :style="'top:'+ barHeight + 'px'" @click="">{{time}}S 关闭</view> -->
		<!-- 图片 -->
		<image class="full" mode="widthFix" src="https://static.newbeaconhotels.com/app/hotel/load.jpg"></image>
	</view>
</template>

<script>
	import {
		Config
	} from '@/common/js/config.js';
	export default {
		data() {
			return {
				firstObj: {}, // 跳转缓存
				sceneID: null, //分享ID
				barHeight: 0, // 导航栏+状态栏的高度
				time: 5, // 广告时间
			}
		},
		onLoad(options) {
			// 隐藏默认菜单
			uni.hideTabBar();
			
			let $this = this;
			let firstObj = uni.getStorageSync("firstObj");
			$this.firstObj = firstObj
			
			// console.log(this.$Route,'chakan')
			
			if (this.$Route.query.scene && firstObj.path=='/pages/home/index') {
				this.sceneID = this.$Route.query.scene
			}
			// #ifdef H5
			if (options.openid) {
				uni.setStorage({
					key: 'openid',
					data: options.openid
				})
				$this.Onfirst()
			} else {
				$this.goAuthorize()
			}
			// #endif

			// #ifdef MP-WEIXIN
			$this.Onfirst()
			// #endif
			
			// 获取导航栏+状态栏的高度
			uni.getSystemInfo({
				success: (e) => {
					this.barHeight = this.customBar
					// #ifdef H5
					this.barHeight = 0
					// #endif
				}
			})
		},
		methods: {
			//首次进入判断
			Onfirst() {
				let that = this
				// 检验token失效
				let token = new Promise((resolved, rejected) => {
					if (uni.getStorageSync('token')) {
						that.$Http({
							url: 'checkToken',
							token: true
						}).then(res => {
							if (res.code == 1) {
								//token正常
								resolved(true)
							} else {
								//强制清空缓存当用户未登录
								uni.removeStorageSync('userInfo')
								uni.removeStorageSync('token')
								resolved(true)
							}
						})
					} else {
						resolved(true)
					}
				})
				//判断版本是不是最新版本
				let renew = new Promise((resolved, rejected) => {
					// #ifdef MP-WEIXIN
					//第一步判断当前getUpdateManager api用户可不可以用
					if (wx.canIUse('getUpdateManager')) {
						const updateManager = wx.getUpdateManager()
						updateManager.onCheckForUpdate(function(res) {
							// 请求完新版本信息的回调
							if (res.hasUpdate) {
								updateManager.onUpdateReady(function() {
									wx.showModal({
										title: '更新提示',
										content: '新版本已经准备好，是否重启应用？',
										showCancel: false,
										confirmColor: '#03004c',
										success: function(res) {
											if (res.confirm) {
												// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
												updateManager.applyUpdate()
											}
										}
									})
								})
								updateManager.onUpdateFailed(function() {
									// 新的版本下载失败
									wx.showModal({
										title: '已经有新版本了哟~',
										content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
										showCancel: false,
										confirmColor: '#03004c',
										success: function(res) {
											if (res.confirm) {
												resolved(true)
											}
										}
									})
								})
							} else {
								//已经是最近版本
								resolved(true)
							}
						})
					} else {
						wx.showModal({
							title: '系统提示~',
							content: '您的微信版本过低,请更新微信,否则小程序部分功能不能使用~',
							showCancel: false,
							confirmColor: '#03004c',
							success: function(res) {
								if (res.confirm) {
									resolved(true)
								}
							}
						})
					}
					// #endif
					// #ifdef H5
					resolved(true)
					// #endif
				})
				//获取常规配置
				let config = new Promise((resolved, rejected) => {
					this.$Http({
						url: 'ver'
					}).then(res => {
						// #ifdef MP-WEIXIN
						if (res.code == 1) {
							let version = wx.getAccountInfoSync().miniProgram.version
							if (res.data.ver == version || res.data.over == version) {
								res.data.conshow = true
							} else {
								// res.data.conshow = true
								res.data.conshow = false
							}
							res.data.version = version
							uni.setStorage({
								key: 'version',
								data: res.data
							})
							resolved(true)
						}
						// #endif

						// #ifdef H5
						resolved(true)
						// #endif
					})
				})
				//如果是分享二维码进来
				let scene = new Promise((resolved, rejected) => {
					if (this.sceneID) {
						// #ifdef MP-WEIXIN
						that.$Http({
							url: 'getQrcode',
							data:{
								id:this.sceneID
							}
						}).then(res => {
							if (res.code == 1) {
								that.firstObj.path = res.data.page
								that.firstObj.query = {
									scene:res.data.message
								}
								that.firstObj.fullPath =  res.data.page + '?scene=' + res.data.message
								resolved(true)
							} else {
								resolved(true)
							}
						})
						// #endif
						// #ifdef H5
						resolved(true)
						// #endif
					} else {
						resolved(true)
					}
				})

				Promise.all([token, renew, config, scene]).then((result) => {
					// 通过二维码进来
					if (that.firstObj.query.scene) {
						that.Onpages(true)
					} else {
						that.Onpages(false)
					}
				}).catch((error) => {})
			},
			// 页面跳转
			Onpages(type) {
				let that = this
				if (type) {
					let obj = this.$base.OnWxoptions(this.firstObj.query.scene);

					// 存储各种id信息
					let ids = {}
					if (obj.Pid) {
						ids.Pid = obj.Pid
					}
					if (obj.Fid) {
						ids.Fid = obj.Fid
					}
					if (obj.Cid) {
						ids.Cid = obj.Cid
					}
					uni.setStorage({ key: 'idInfo_origin', data: ids })
					uni.setStorage({ key: 'idInfo', data: ids })

					let num1 = 0; // 所有参数的个数
					let num2 = 0; // 基础参数的个数
					let query = {}; // 基础参数
					for (let key in obj) {
						num1++
						if (key != 'Pid' && key != 'Uid' && key != 'Fid' && key != 'Cid') {
							num2++
							query[key] = obj[key]
						}
					}

					let page = ''; // 跳转的路径
					if (num2 == 0) {
						// 基础参数 = 0，只有固定参数
						page = that.firstObj.path
					} else if (num2 == 1) {
						// 基础参数 = 1，直接用 ? 拼接
						for (let key in query) {
							page = that.firstObj.path + '?' + key + '=' + query[key]
						}
					} else {
						// 基础参数 > 1，用 ?data= 拼接
						let h5url = ''
						for (let key in query) {
							h5url += key + '.' + query[key] + ','
						}
						h5url = h5url.slice(0, h5url.length - 1)
						page = that.firstObj.path + '?data=' + h5url
					}

					// #ifdef MP-WEIXIN
					that.$Router.replaceAll(page)
					// #endif

					// #ifdef H5
					uni.setStorage({
						key: 'firstType',
						data: true
					})
					window.location.href = Config.http_h5_url + page
					// #endif
				} else {
					// #ifdef MP-WEIXIN
					that.$Router.replaceAll({
						path: that.firstObj.path,
						query: that.firstObj.query
					})
					// #endif

					// #ifdef H5
					uni.setStorage({
						key: 'firstType',
						data: true
					})
					window.location.href = Config.http_h5_url + that.firstObj.fullPath
					// #endif
				}
			},
			// H5授权
			goAuthorize() {
				let $url = window.location.href;
				let $return_url = $url.replace(/&/g, ",");
				let jump_url = 'https://jd.newbeaconhotels.com/api/Oauth?return_url=' + $return_url;
				uni.setStorage({
					key: 'firstType',
					data: true,
				})
				window.location.href = jump_url
			},
		}
	}
</script>

<style lang="less">
	@import url("style.less");
</style>
